package com.enthal.basicpers

import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import permissions.dispatcher.RuntimePermissions


/**
 * @author : zlf
 * date   : 2019/1/22
 * blog   :https://www.jianshu.com/u/281e9668a5a6
 * 1，在需要动态申请权限的类上添加 @RuntimePermissions 的注解
 */
@RuntimePermissions
class FirstFragment : Fragment() {
    // 锁屏广播接收器
    private val mScreenReceiver = DynamicReceiver()

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        view.findViewById<Button>(R.id.button_first).setOnClickListener {
            findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        }

        // 动态注册
        val filter = IntentFilter()
        filter.addAction(Intent.ACTION_SCREEN_ON)
        filter.addAction(Intent.ACTION_SCREEN_OFF)
        filter.addAction(Intent.ACTION_USER_PRESENT)
        requireContext().registerReceiver(mScreenReceiver, filter)

        val rtpers = RuntimePers(requireContext())
        val imei = rtpers.getPhoneIMEI()
    //    Toast.makeText(requireContext(), imei, Toast.LENGTH_SHORT).show()

        val wifiManager =
            view.context.applicationContext.getSystemService(Service.WIFI_SERVICE) as WifiManager
        val wifiinfo = wifiManager.connectionInfo
     //   Toast.makeText(requireContext(), wifiinfo.toString(), Toast.LENGTH_LONG).show()

        val isconn = RuntimeStatus.iNetConnected(view.context)
      //  Toast.makeText(requireContext(), "Conn Status: $isconn", Toast.LENGTH_LONG).show()

        val iswificonn = RuntimeStatus.isWifiConnected(view.context)
     //   Toast.makeText(requireContext(), "WIFI Conn Status: $iswificonn", Toast.LENGTH_LONG).show()

        startAllServices()
        toggleNotificationListenerService(requireContext())
    }

    override fun onDestroy() {
        requireContext().unregisterReceiver(mScreenReceiver)
        super.onDestroy()
    }

    /**
     * 开启所有Service
     */
    private fun startAllServices() {
        val intent1 = Intent(requireContext(), NoticeListenerService::class.java)
     //   val intent2 = Intent(requireContext(), GuardService::class.java)

        RuntimeStatus.startIndent(requireContext(), intent1)
     //   startIndent(intent2)
    }

    // 在退出app后，再次打开，监听不生效，这个时候我们需要做一些处理。
    // 在app启动时，我们去重新关闭打开一次监听服务，让它正常工作。
    private fun toggleNotificationListenerService(context: Context) {
        val pm: PackageManager = context.packageManager
        pm.setComponentEnabledSetting(
            ComponentName(context, NoticeListenerService::class.java),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
        )
        pm.setComponentEnabledSetting(
            ComponentName(context, NoticeListenerService::class.java),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
        )
    }


}